package servlet;

import bean.Movie;
import service.impl.MovieServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/user/get_movie")
public class GetMovieServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String movieIdStr = request.getParameter("movieId");
        if (movieIdStr != null && !movieIdStr.isEmpty()) {
            int movieId = Integer.parseInt(movieIdStr);
            // 在这里处理 movieId，例如从数据库中获取电影信息  
            // ...  
            MovieServiceImpl movieService = new MovieServiceImpl();
            Movie movie  = movieService.getMovieById(movieId);
            if (movie == null){
                System.out.println("获取对象失败");
            }else {
                request.getSession().setAttribute("movie",movie);
            }
            // 假设您只是将 movieId 返回给客户端  
            response.setContentType("text/plain");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().println("Movie ID: " + movieId);
        } else {
            // 处理无效的 movieId  
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            response.getWriter().println("Invalid movie ID.");
        }
    }
}